<template>
    <div class="equipInfo-container">
		    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			    <el-form :model="queryParams" ref="queryForm" :inline="true">
			        <el-row :gutter="35">
                        <el-col :xs="24" :sm="12" :md="8" :lg="7" :xl="6" class="mb20 search-actions">
                            <el-form-item label="Mqtt状态">
                                <el-tag :type="mqttStatusColor" round effect="dark" > {{mqttStatus}} </el-tag>
                            </el-form-item>
                            <el-form-item label="间隔时间">
                                <el-input v-model="queryParams.interval" clearable placeholder="毫秒"/>
                            </el-form-item>
                        </el-col>
                      
                        <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="6" class="mb20 search-actions">                            
                            <div>                                
                                <el-button icon="ele-CaretRight" @click="MqttStart" > 启动 </el-button>
                                <el-button icon="ele-Close" @click="MqttStop" > 停止 </el-button>  
                                <el-button icon="ele-Refresh" @click="mqttServerStatus"> 刷新 </el-button>
                            </div>                            
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6" class="mb20 search-actions">
                            <div>
                                <el-button type="primary" plain icon="ele-Search" @click="handleQuery" v-auth="'equipInfo:page'"> 查询客户端 </el-button>
                            </div>
                        </el-col>
                    </el-row>
                </el-form>
		    </el-card>
            <el-card shadow="hover" style="margin-top: 8px">
                <el-table :data="tableData"	style="width: 100%"	v-loading="loading"	tooltip-effect="light" row-key="id"	border="">
                    <el-table-column type="index" label="序号" width="55" align="center" fixed="" />
                    <el-table-column prop="endpoint" label="IP" fixed="" show-overflow-tooltip="" />
                    <el-table-column prop="id" label="客户端ID" fixed="" show-overflow-tooltip="" />
                    <el-table-column prop="connectedTimestamp" label="连接时间" fixed="" show-overflow-tooltip="" />	
                    <el-table-column prop="lastPacketReceivedTimestamp" label="最近接收时间" fixed="" show-overflow-tooltip="" />	
                    <el-table-column prop="lastPacketSentTimestamp" label="最近发送时间" fixed="" show-overflow-tooltip="" />	
                    <el-table-column prop="lastNonKeepAlivePacketReceivedTimestamp" label="最近接收数据时间" fixed="" show-overflow-tooltip="" />		
                    
                </el-table>
                <el-pagination
                    v-model:currentPage="tableParams.page"
                    v-model:page-size="tableParams.pageSize"
                    :total="tableParams.total"
                    :page-sizes="[100]"
                    small=""
                    background=""
                    layout="total, sizes, prev, pager, next, jumper"/>
                
            </el-card>
        </div>
</template>

<script lang="ts" setup>
    import { ref } from "vue";
    import { ElMessageBox, ElMessage } from "element-plus";
    import { auth } from '/@/utils/authFunction';
    //import { formatDate } from '/@/utils/formatTime';

    import { getMqttClientInfoPageList,mqttStart,mqttStop,getMqttServerStatus } from '/@/api/main/mqtt';
import { log } from "console";
import internal from "stream";

    const isShow=ref(false);
    const editDialogRef = ref();
    const loading = ref(false);
    const tableData = ref<any>([]);
    const queryParams = ref({
        interval: 1000
    });
    const tableParams = ref({
        page: 1,
        pageSize: 100,
        total: 0,
    });
    const isMqttStarted=ref(false);
    
    const mqttStatus=ref("停止");
    const mqttStatusColor=ref("failure");
    // 查询操作
    const handleQuery = async () => {
        if(isMqttStarted){
            loading.value = true;
            var res = await getMqttClientInfoPageList(Object.assign(queryParams.value, tableParams.value));
            tableData.value = res.data.result?.items ?? [];
            tableParams.value.total = res.data.result?.total;
            //console.log(tableData.value[0]);
            loading.value = false;
        }
        
    };
    // 查询Mqtt状态
    const mqttServerStatus = async () => {
        var res = await getMqttServerStatus();
        
        isMqttStarted.value = res.data.result.started;
        //console.log("aaa-" + JSON.stringify(res.data, null, 2));
        queryParams.value.interval =  res.data.result.interval;
 
        if(res.data.result.started == true){
            mqttStatus.value="启动";
            mqttStatusColor.value = "success";
        }
        else if(res.data.result.started == false)
        {
            mqttStatus.value="停止";
            mqttStatusColor.value = "failure";
        }
        loading.value = false;
    };

    const MqttStart = async () => {
    
        var res = await mqttStart(queryParams.value);
        if(res.data.result.commandResult == true && res.data.result.started == true){
            mqttStatus.value="启动";
            mqttStatusColor.value = "success";
        }
        
    }

    const MqttStop = async() => {
        
        var res = await mqttStop();
        if(res.data.result.commandResult == true && res.data.result.started == false){
            mqttStatus.value="停止";
            mqttStatusColor.value = "failure";
        }
        
    }
    mqttServerStatus();
    handleQuery();
</script>

